import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:ly/model/cartmodellist.dart';
import '../config/api.dart';
class HomeDetail extends StatefulWidget {
  Map arguments;
  HomeDetail({Key key, this.arguments}) : super(key: key);

  @override
  _HomeDetailState createState() => _HomeDetailState(arguments: this.arguments);
}

class _HomeDetailState extends State<HomeDetail> {
  Map arguments;
  List c=[1,2,3];
  _HomeDetailState({this.arguments});
     @override
void initState() {
  super.initState();
  getAlist();
}
List goods;
List goodl;
bool flag=false;
void getAlist()async{
  var v=await getLungoods();
  print(v.data['message']);
  setState(() {
    flag=true;
    goods=v.data['message'];
    
  });
  // getLungoods().then((value) => {
  //   setState((){
  //     goods=value.data['message'];
  //    // goodl=value.data['message'];
  //     //goodl=goodl.reversed;
  //    // goodl.length=3;
  //   }),
  //   print(goods),
  //  // print(goodl)
  // });
}
  //特价商品
  List <Widget> bar()=>List.generate(3, (index) {
        return Container(
         width: ScreenUtil().setWidth(220),
         height: ScreenUtil().setHeight(220),
         child: InkWell(
           onTap: (){
              Navigator.pushNamed(context, '/goodscommodity',arguments: {"id":goods[index]});
           },
           child: Card(
           child: Column(
             mainAxisAlignment: MainAxisAlignment.spaceBetween,
             children: <Widget>[
               Container(
                 width: ScreenUtil().setWidth(220),
                  height: ScreenUtil().setHeight(160),
                  //goods[index]['url'].split(',')[0]http://dbyxs.xyz:3005/uploads/fae4e814a5d9510d10277ff78ea6dc12
                 child: Image.network(goods[index]['url'].split(',')[0],fit: BoxFit.cover,),
               ),
               RichText(text: TextSpan(
                 text: '活动价',
                 style: TextStyle(
                   color: Colors.red,
                   fontSize: 12,
                   fontWeight: FontWeight.w300
                  // backgroundColor: Colors.red
                 ),
                 children: [
                   TextSpan(
                     text:'${goods[index]['price']}元  ',
                     style: TextStyle(
                       color: Colors.red,
                       fontSize: 10
                     )
                   )
                 ])),
                 SizedBox(height: 2,)
             ],
           ),
         ),
         ) 
        );
  });
  //商品主体
  List <Widget> barlist()=> List.generate(goods.length, (index) {
      return Container(
        height: ScreenUtil().setHeight(400),
        width: ScreenUtil().setWidth(240),
        child: 
       InkWell(
         onTap: (){
           print(goods[index]);
           Navigator.pushNamed(context, '/goodscommodity',arguments: {"id":goods[index]});
           //http://dbyxs.xyz:3005/uploads/b593883c3adac89564d762ae7b76ad5e
         },
         child:  Card(
          child: Column(
            children: <Widget>[
                Container(
                  width: ScreenUtil().setWidth(230),
                  height: ScreenUtil().setHeight(250),
                  child: Image.network(goods[index]['url'].split(',')[0],fit:BoxFit.cover),
                ),
                Text('${goods[index]['goodsname']}',maxLines: 1,overflow: TextOverflow.ellipsis,style: TextStyle(fontSize:12,color:Colors.black38),),
                Container(
                  margin: EdgeInsets.only(right:10,left: 10),
                  height: ScreenUtil().setHeight(40),
                  decoration: BoxDecoration(
                    border: Border.all(width:1,color:Colors.red),
                    borderRadius: BorderRadius.circular(2)
                  ),
                  child: Center(child: Text('限时优惠',style: TextStyle(color:Colors.red[300],fontSize:12,fontWeight: FontWeight.w500),),),
                ),
                SizedBox(height: 4,),
                 RichText(text: TextSpan(
                 text: '${goods[index]['price']}元  ',
                 style: TextStyle(
                   color: Colors.red[300],
                   fontSize: 12,
                   fontWeight: FontWeight.w300
                  // backgroundColor: Colors.red
                 ),
                 
                 children: [
                   TextSpan(
                     text:' ${goods[index]['price']+100}元',
                     style: TextStyle(
                       color: Colors.black38,
                       decoration: TextDecoration.lineThrough,
                       fontSize: 10
                     )
                   )
                 ])),
            ],
          ),
        ),
       )
      );
  });
  @override
  Widget build(BuildContext context) {
   if(goods==null){
     return Container(
       child: Scaffold(
         body: Center(child:Text('数据加载中')),
       ),
     );
   }else{
      return Container(
        decoration: BoxDecoration(),
        child: Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.white,
            leading: new IconButton(
               icon: new Icon(Icons.arrow_back, color: Colors.black),
               onPressed: () => Navigator.of(context).pop(),
              ),
            title: Center(
              child:Text(
                '商品列表',style: TextStyle(color:Colors.orangeAccent),
              )),
            textTheme: TextTheme(),
          ),
          body: Container(
            height: double.infinity,
            width: double.infinity,
            color: Colors.pink[400],
            child: 
           ListView(
             children: <Widget>[
                Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Container(
                  child: Image.network('http://dbyxs.xyz:3005/uploads/27879a788a9b2c06f9cb1409761f1d17',fit:BoxFit.cover)
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: bar(),
                ),
                Container(
                  width: ScreenUtil().setWidth(710),
                  height: ScreenUtil().setHeight(110),
                 // margin: EdgeInsets.only(bottom:15),
                  child: Card(
                    child: ListTile(
                      title: Text('新品上架',style:TextStyle(color:Colors.black38,fontSize: 16,fontWeight: FontWeight.w500)),
                      trailing: Icon(Icons.arrow_right,size: 34,),
                    ),
                  ),
                ),
                Wrap(
                  spacing: 0,
                  children: goods==[]?Center(child:Text('加载中')):barlist(),
                )
              ],
            ),
             ],
           )
          ),
        ));
   }
  }
}
